package com.example.erat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 系统控制器
 * 
 * 提供系统状态和诊断信息的接口
 * 质量属性关联：
 * - 可测试性：提供系统状态检查的接口
 * - 可维护性：便于系统故障诊断
 */
@RestController
@RequestMapping("/api/system")
public class SystemController {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public SystemController(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 获取系统状态信息
     * 
     * @return 包含系统状态信息的Map
     */
    @GetMapping("/status")
    public Map<String, Object> getSystemStatus() {
        Map<String, Object> status = new HashMap<>();
        status.put("status", "running");
        status.put("timestamp", System.currentTimeMillis());
        
        try {
            // 测试数据库连接
            String dbVersion = jdbcTemplate.queryForObject("SELECT VERSION()", String.class);
            status.put("database", "connected");
            status.put("databaseVersion", dbVersion);
        } catch (Exception e) {
            status.put("database", "error");
            status.put("error", e.getMessage());
        }
        
        return status;
    }
} 